<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true" @submit.native.prevent>
                <el-form-item :label="$t('语言名称')">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" :placeholder="$t('语言名称')" clearable
                        @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">{{ $t('查询') }}</el-button>
                    <el-button @click="resetParams">{{ $t('重置') }}</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['setting/lang/lang/add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    {{ $t('新增') }}
                </el-button>
            </div>
            <el-table size="large" class="mt-4" v-loading="pager.loading" :data="pager.lists">
                <el-table-column :label="$t('ID')" prop="id" min-width="120" />
                <el-table-column :label="$t('语言名称')" prop="language_name" min-width="100" />
                <el-table-column :label="$t('浏览器识别码')" prop="file_name" min-width="120" />
                <el-table-column :label="$t('状态')" prop="status" min-width="100">
                    <template #default="{ row }">
                        <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                            @change="changeStatus(row.id, row.status)" :disabled="row.is_default === 1 ? true : false" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('默认')" prop="is_default" min-width="100">
                    <template #default="{ row }">
                        <el-switch v-model="row.is_default" :active-value="1" :inactive-value="0"
                            @change="changeDefault(row.id, row.is_default)"
                            :disabled="row.is_default === 1 ? true : false" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('操作')" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['setting/lang/lang/detail']" type="primary" link @click="handleEdit(row)">
                            {{ $t('编辑') }}
                        </el-button>
                        <el-button v-if="row.pid !== 0" v-perms="['setting/lang/lang/delete']" type="danger" link
                            @click="handleDelete(row.id)">
                            {{ $t('删除') }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="lang">
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import useLangStore from '@/stores/modules/lang'
import { langLists, langDelete, langChangeStatus, langChangeDefault } from '@/api/setting/lang'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
import EditPopup from './edit.vue'

const langStore = useLangStore()

const queryParams = reactive({
    keyword: ''
})
const showEdit = ref(false)

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: langLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm(t('确定要删除?'))
    await langDelete({ id })
    getLists()
}

const changeStatus = async (id: number, status: number) => {
    try {
        await langChangeStatus({ id: id, status: status })
    } catch (error) { }

    await langStore.getLangConfig()
    getLists()
}

const changeDefault = async (id: number, is_default: number) => {
    try {
        await langChangeDefault({ id: id, is_default: is_default })
    } catch (error) { }

    await langStore.getLangConfig()
    getLists()
}

getLists()
</script>
